package com.pat.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.pat.manager.MobilePhoneManager;
import com.pat.om.MobilePhone;

@Path("/mobilePhones/")
public class MobilePhoneService {

	private MobilePhoneManager mobilePhoneMgr = new MobilePhoneManager();

	@GET
	@Path("/all")
	@Produces({ MediaType.APPLICATION_JSON })
	public List<MobilePhone> get() {
		return mobilePhoneMgr.get();
	}

	@GET
	@Path("/mobilePhone/{number}")
	@Produces({ MediaType.APPLICATION_JSON })
	public List<MobilePhone> get(@PathParam("number") String number) {
		return mobilePhoneMgr.get(number);
	}

	@POST
	@Path("/mobilePhone")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response add(MobilePhone mobilePhone) {
		mobilePhoneMgr.add(mobilePhone);
		return Response.status(201).entity(mobilePhone).build();
	}

	@DELETE
	@Path("/mobilePhone/{number}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response delete(@PathParam("number") String number) {
		int result = mobilePhoneMgr.delete(number);
		if (result > 0) {
			return Response
					.status(200)
					.entity(String.valueOf(result)).build();
		} else {
			return Response
					.status(304)
					.entity(0).build();
		}

	}

	@PUT
	@Path("/mobilePhone/")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateLocationInJSON(MobilePhone mobilePhone) {
		mobilePhoneMgr.update(mobilePhone);
		return Response.status(201).entity(mobilePhone).build();
	}
}
